package main

import (
	"flag"
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"os"
)

var (
	port int
	tls  bool
	certFile string
	keyFile string

	address string
)

func init() {
	flag.Parse()
	flag.IntVar(&port, "p", 8080, "port for service")
	flag.StringVar(&certFile, "cert", "", "certificate file for service if tls is enabled")
	flag.StringVar(&keyFile, "key", "", "key file for service if tls is enabled")

	address = fmt.Sprintf(":%d", port)

	if tls {
		if certFile == "" {
			certFile = os.Getenv("WEB_DEMO_SERVER_CERT")
		}

		if keyFile == "" {
			keyFile = os.Getenv("WEB_DEMO_SERVER_KEY")
		}
	}
}

func main() {
	fmt.Println("Web Demo Service Start")
	engine := gin.Default()
	engine.Static("static/image", "./static")
	engine.LoadHTMLFiles("./static/main.html")
	engine.GET("/", func(c *gin.Context) {
		c.HTML(200, "main.html", nil)
	})
	engine.GET("/image/icon-godzilla.png", func(c *gin.Context) {
		img, _ := ioutil.ReadFile("./static/image/icon-godzilla.png")
		c.Writer.Write(img)
	})
	
	engine.GET("/ping", func(c *gin.Context) {

	})

	if tls {
		engine.RunTLS(":9443", certFile, keyFile)
	} else {
		engine.Run(":9090")
	}
}
